package com.xuanwei.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.admin.handler.CrawlSNProductHandler;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.entity.ao.ProductAttributeAO;
import com.xuanwei.core.entity.ao.ProductSkuAO;
import com.xuanwei.core.entity.vo.ProductListVo;
import com.xuanwei.core.entity.param.ProductParam;
import com.xuanwei.core.service.IXwProductService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.dao.entity.XwProduct;
import com.xuanwei.dao.entity.XwProductParam;
import com.xuanwei.dao.entity.XwProductSku;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2019-04-25
 */
@RestController
@RequestMapping("/api/product")
public class ProductController extends BasicController {

    private IXwProductService productService;

    private CrawlSNProductHandler crawlSNProductHandler;

    public ProductController(IXwProductService productService, CrawlSNProductHandler crawlSNProductHandler) {
        this.productService = productService;
        this.crawlSNProductHandler = crawlSNProductHandler;
    }

    /**
     * 添加商品
     *
     * @param productParam 参数
     * @return 响应
     */
    @PostMapping("add.do")
    public ResultVO add(@RequestBody ProductParam productParam) {
        if (productParam == null) {
            return ResultVO.failResult("参数为空");
        }
        XwProduct product = productParam.getProduct();
        if (product == null) {
            return ResultVO.failResult("基本信息为空");
        }

        List<XwProductParam> params = productParam.getParams();
        List<ProductAttributeAO> productAttributes = productParam.getProductAttributes();
        if (productAttributes == null) {
            return ResultVO.failResult("商品属性为空");
        }
        List<ProductSkuAO> productSkus = productParam.getProductSkus();
        if (productSkus == null) {
            return ResultVO.failResult("商品SKU为空");
        }
        if (productParam.getProduct().getId() != null && productParam.getProduct().getId() > 0) {
            productService.update(product, getId(), params, productAttributes, productSkus);
            return ResultVO.successResult("修改成功");
        } else {
            productService.add(product, getId(), params, productAttributes, productSkus);
            return ResultVO.successResult("添加成功");
        }


    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list(String name, Integer status, Integer orderBy) {
        Page<XwProduct> page = getPage();
        Page<ProductListVo> pageData = productService.findPage(page, name, status, orderBy);
        return ResultVO.successResult(pageData);
    }

    @RequestMapping(value = "changeStatus.do", method = RequestMethod.GET)
    public ResultVO changeStatus(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {

            return ResultVO.failResult("传入ID不正确");
        }
        productService.handleStatus(id);
        return ResultVO.successResult("修改成功");
    }


    @RequestMapping(value = "changeLabels.do", method = RequestMethod.GET)
    public ResultVO changeLabels(Integer id, Integer labelId) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("传入ID不正确");
        }
        productService.updateLabel(id, labelId);
        return ResultVO.successResult("修改成功");
    }


    @RequestMapping(value = "findSku.do", method = RequestMethod.GET)
    public ResultVO findSku(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("传入ID不正确");
        }
        List<ProductSkuAO> skuAOS = productService.findSkuByProductId(id);
        return ResultVO.successResult(skuAOS);
    }


    @RequestMapping(value = "updateSku.do", method = RequestMethod.POST)
    public ResultVO updateSku(@RequestBody XwProductSku productSku) {
        if (productSku == null) {
            return ResultVO.failResult("参数错误");
        }
        productService.updateSku(productSku, getId());
        return ResultVO.successResult("修改成功");
    }


    @RequestMapping(value = "changeSkuStatus.do", method = RequestMethod.GET)
    public ResultVO changeSkuStatus(Integer skuId) {
        if (!ParamUtil.checkIsPositiveInteger(skuId)) {
            return ResultVO.failResult("传入ID不正确");
        }
        productService.updateSkuStatus(skuId);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "productParams.do", method = RequestMethod.GET)
    public ResultVO productParams(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("传入ID不正确");
        }
        List<XwProductParam> params = productService.findParams(id);
        return ResultVO.successResult(params);
    }

    @RequestMapping(value = "updateParams.do", method = RequestMethod.GET)
    public ResultVO updateParams(Integer type, Integer id, Integer productId, String name, String value) {
        if (!ParamUtil.checkIsContainArray(type, 1, 2, 3)) {
            return ResultVO.failResult("type参数不正确");
        }
        productService.updateParams(type, id, productId, name, value);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "productDetail.do", method = RequestMethod.GET)
    public ResultVO productDetail(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数不正确");
        }
        return ResultVO.successResult("", productService.findDetail(id));
    }

    @RequestMapping(value = "updateDetail.do", method = RequestMethod.POST)
    public ResultVO updateDetail(Integer id, String detail) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数不正确");
        }
        productService.updateDetail(id, detail, getId());
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    public ResultVO detail(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数不正确");
        }
        ProductParam productParam = productService.editDetail(id);
        return ResultVO.successResult(productParam);
    }

    @RequestMapping(value = "getCrawlDetail.do", method = RequestMethod.GET)
    public ResultVO addFormSJS(String url) {
        if (StringUtils.isBlank(url)) {
            ResultVO.failResult("请输入链接");
        }
        return ResultVO.successResult(crawlSNProductHandler.getDetail(url));
    }

    @RequestMapping(value = "updateName.do", method = RequestMethod.GET)
    public ResultVO updateName(Integer id, String name) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数为空");
        }

        if (StringUtils.isBlank(name)) {
            return ResultVO.failResult("商品名称不能为空");
        }

        XwProduct product = new XwProduct();
        product.setId(id);
        product.setName(name);
        product.setUpdateTime(LocalDateTime.now());

        productService.updateById(product);

        return ResultVO.successResult("修改成功");
    }

}